package com.fantabest.common.server.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.fantabest.common.PMF;
import com.fantabest.common.entity.Manager;

public class ManagerDao {

	private static ManagerDao instance;

	public static ManagerDao getInstance() {
		if(instance == null)
			instance = new ManagerDao();
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public Manager load(String email, String pwd)	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Manager.class);
		q.setFilter("email == pemail");
		q.setFilter("pwd == ppwd");
		q.declareParameters("String pemail, String ppwd");
		Manager manager = null;
		List<Manager> result = null;
		try {
			result = (List<Manager>)q.execute(email,pwd);	
			if(result.size()>0)	{
				manager = result.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	finally	{
			q.closeAll();
		}
        return manager;
	}
	
	public void save(String name, String surname, String nickname, String email, String pwd)	{
		Manager manager = new Manager(name, surname, nickname, email, pwd);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(manager);
		} catch (Exception e) {
			e.printStackTrace();
		}	finally	{
			pm.close();
		}
	}
	
	
}
